<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<div style="margin: auto;text-align: center">
    <input id="sensorId" type="number"/>
    <button onclick="send()">查询</button>
</div>
<body>
    <script type="text/javascript">
        var socket;
        if (typeof (WebSocket) != "undefined") {//表示浏览器支持websocket
            //创建WebSocket对象,ws对应http,必须写作"ws"两个字符,要指定服务器的地址和端口
            socket = new WebSocket("ws://localhost:8081/monitor");
            //连接打开事件
            socket.onopen = function() {
                //在Server中的onMessage方法会收到这条消息
                socket.send("Socket is open");
            };
            //收到消息事件
            socket.onmessage = function(msg) {
                alert("收到消息："+msg.data)
                console.log(msg.data);
            };
            //连接关闭事件
            socket.onclose = function() {
                console.log("Socket已关闭");
            };
            //发生了错误事件
            socket.onerror = function() {
                alert("Socket发生了错误");
            }

            //窗口关闭时，关闭连接
            window.unload=function() {
                socket.close();
            };
        }
        function send() {
            socket.send(document.getElementById('sensorId').value);
        }
    </script>
</body>
</html>